// 定义一组预定义的颜色数组，用于在各种场景中轻松引用这些颜色
// 这些颜色包括从白色到黑色的不同灰度，以及一些鲜艳的颜色，格式有十六进制、RGB、RGBA、HSV、HSL等
export const predefine_colors = ['#fff', '#ddd', '#ccc', '#999', '#666', '#333', '#000', '#ff4500', '#ff8c00', '#ffd700', '#90ee90', '#00ced1', '#c71585', 'rgba(255, 69, 0, 0.68)', 'rgb(255, 120, 0)', 'hsv(51, 100, 98)', 'hsva(120, 40, 94, 0.5)', 'hsl(181, 100%, 37%)', '#1F93FF', '#c7158577'];
// 数据的默认值，避免没有值的时候报错
export const old_radius = {
    radius: 0,
    radius_top_left: 0,
    radius_top_right: 0,
    radius_bottom_left: 0,
    radius_bottom_right: 0,
};

export const old_padding = { padding: 0, padding_top: 0, padding_bottom: 0, padding_left: 0, padding_right: 0 };

export const old_margin = { margin: 0, margin_top: 0, margin_bottom: 0, margin_left: 0, margin_right: 0 };

export const old_border_and_box_shadow = {
    border_is_show: '0',
    border_color: '#FF3F3F',
    border_style: 'solid',
    border_size: { padding: 1, padding_top: 1, padding_right: 1, padding_bottom: 1, padding_left: 1 },
    box_shadow_color: '',
    box_shadow_x: 0,
    box_shadow_y: 0,
    box_shadow_blur: 0,
    box_shadow_spread: 0,
};
// 只好设置
export const font_weight = [
    { name: '加粗', value: 'bold' },
    { name: '正常', value: '400' },
];


export function transformParams(params: any) {
    const result: any[] = [];
    if (params != null && typeof params === 'object') {
        Object.keys(params).forEach((key) => {
            const value = params[key];
            if (value != null && value !== '') {
                if (typeof value === 'object' && !isBlobFile(value)) {
                    getObjectParamsArray(value).forEach((item) => {
                        result.push([`${key}${item[0]}`, item[1]]);
                    });
                } else {
                    result.push([key, value]);
                }
            }
        });
    }
    return result;
}

export function getObjectParamsArray(obj: any) {
    const result: any[] = [];
    Object.keys(obj).forEach((key) => {
        const value = obj[key];
        if (value != null && value !== '') {
            const name = `[${key}]`;
            if (typeof value === 'object' && !isBlobFile(value)) {
                getObjectParamsArray(value).forEach((item) => {
                    result.push([`${name}${item[0]}`, item[1]]);
                });
            } else {
                result.push([name, value]);
            }
        }
    });
    return result;
}

/**
 * 判断是否是文件
 * @param obj 对象
 */
export function isBlobFile(obj: any) {
    return obj != null && (obj instanceof Blob || obj instanceof File);
}

export function toURLSearch(params: any, url: any) {
    if (typeof params !== 'object' || params == null) {
        return '';
    }
    const result = transformParams(params)
        .map((d) => `${encodeURIComponent(d[0])}=${encodeURIComponent(d[1])}`)
        .join('&');
    if (!url) {
        return result;
    }
    return (url.includes('?') ? `${url}&` : `${url}?`) + result;
}
